package com.MVC.dao;


import com.MVC.Util.JDBCUtils;
import com.MVC.entity.Student;

import java.util.List;

public class StudentDAOImpl  implements StudentDAO {
    @Override
    public List<Student> selectAllStudents() {
        return JDBCUtils.query(Student.class, "select * from student");
    }

    @Override
    public List<Student> selectStudentByWord(String word) {
         List<Student> student = JDBCUtils.query(Student.class,"select * from student where name like ?","%"+word+"%");
        if (student.size()>0){
            return student;
        }
        return null;
    }

    @Override
    public int selectStudentsCount() {
        return JDBCUtils.queryCount("select count(*) from student");
    }

    @Override
    public List<Student> selectStudentsPage(int start, int len) {
        return JDBCUtils.query(Student.class, "select * from student limit ?,?", start, len);
    }

    @Override
    public Student selectStudentById(int id) {
        List<Student> students = JDBCUtils.query(Student.class, "select * from student where id =?", id);
        if (students != null && students.size() > 0) {
            return students.get(0);
        }
        return null;
    }

    @Override
    public void insertStudent(Student student) {
        JDBCUtils.update("insert into student(name,age,gender,idcard,address) values(?,?,?,?,?) ",
                student.getName(), student.getAge(), student.getGender(), student.getIdcard(), student.getAddress());
    }

    @Override
    public void updateStudentById(Student student) {
        JDBCUtils.update("update student set name=?,age=?,gender=?,idcard=?,address=? where id=?",
                student.getName(), student.getAge(), student.getGender(), student.getIdcard(), student.getAddress(), student.getId());
    }

    @Override
    public void deleteStudentById(int id) {
        JDBCUtils.update("delete from student where id =?", id);
    }
}

